{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-05-13T06:36:43.948678Z",
     "start_time": "2024-05-13T06:36:26.605092Z"
    }
   },
   "source": [
    "# 安装BeautifulSoup，安装本地向量库FAISS\n",
    "! pip install beautifulsoup4 faiss-cpu"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://mirrors.aliyun.com/pypi/simple/\n",
      "Requirement already satisfied: beautifulsoup4 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (4.12.3)\n",
      "Requirement already satisfied: faiss-cpu in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (1.8.0)\n",
      "Requirement already satisfied: soupsieve>1.2 in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from beautifulsoup4) (2.5)\n",
      "Requirement already satisfied: numpy in d:\\developer\\pycharmprojects\\ai-study\\langchain\\venv\\lib\\site-packages (from faiss-cpu) (1.26.4)\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-13T06:36:45.875357Z",
     "start_time": "2024-05-13T06:36:44.939318Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用WebBaseLoader加载知识库\n",
    "from langchain_community.document_loaders import WebBaseLoader\n",
    "loader = WebBaseLoader(\"https://docs.smith.langchain.com/user_guide\")\n",
    "docs = loader.load()"
   ],
   "id": "92e107a534dde296",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-13T06:38:32.573446Z",
     "start_time": "2024-05-13T06:36:45.879356Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用llama3模型，llama3本地模型使用[ollama](https://ollama.com)安装\n",
    "from langchain_community.llms import Ollama\n",
    "llm = Ollama(model=\"llama3\")\n",
    "\n",
    "# 加载提示模板\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "# 加载嵌入功能\n",
    "from langchain_community.embeddings import OllamaEmbeddings\n",
    "embeddings = OllamaEmbeddings()\n",
    "\n",
    "# 建立索引\n",
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
    "text_splitter = RecursiveCharacterTextSplitter()\n",
    "documents = text_splitter.split_documents(docs)\n",
    "vector = FAISS.from_documents(documents, embeddings)"
   ],
   "id": "bdfa8e5cf4be5dd6",
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-13T06:39:20.582710Z",
     "start_time": "2024-05-13T06:38:32.580447Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 建立一个链，可以接受问题和检索到的文档并生成答案\n",
    "from langchain.chains.combine_documents import create_stuff_documents_chain\n",
    "prompt = ChatPromptTemplate.from_template(\"\"\"Answer the following question based only on the provided context:\n",
    "\n",
    "<context>\n",
    "{context}\n",
    "</context>\n",
    "\n",
    "Question: {input}\"\"\")\n",
    "document_chain = create_stuff_documents_chain(llm, prompt)\n",
    "\n",
    "# 可以通过直接传入文档来运行它\n",
    "from langchain_core.documents import Document\n",
    "document_chain.invoke({\n",
    "    \"input\": \"how can langsmith help with testing?\",\n",
    "    \"context\": [Document(page_content=\"langsmith can let you visualize test results\")]\n",
    "})"
   ],
   "id": "2ec40163baf0c121",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'According to the provided context, LangSmith can let you \"visualize test results\". Therefore, LangSmith can help with testing by allowing you to visualize the results of your tests.'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-13T06:46:19.897066Z",
     "start_time": "2024-05-13T06:39:20.586860Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用检索器动态选择最相关的文档并将其传递给给定的问题\n",
    "from langchain.chains import create_retrieval_chain\n",
    "retriever = vector.as_retriever()\n",
    "retrieval_chain = create_retrieval_chain(retriever, document_chain)\n",
    "\n",
    "# 调用这个链。这将返回一个字典 - 来自 LLM 的响应位于answer键中\n",
    "response = retrieval_chain.invoke({\"input\": \"how can langsmith help with testing?\"})\n",
    "print(response[\"answer\"])"
   ],
   "id": "e1ee5d313d6b874d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Based on the provided context, LangSmith can help with testing in the following ways:\n",
      "\n",
      "1. **Capturing Feedback**: LangSmith allows you to gather human feedback on responses produced by your application and attach feedback scores to logged traces. This helps draw attention to problematic runs and highlights edge cases.\n",
      "2. **Annotating Traces**: LangSmith supports sending runs to annotation queues, allowing annotators (e.g., PMs, engineers, or subject matter experts) to closely inspect interesting traces and annotate them with respect to different criteria.\n",
      "3. **Adding Runs to a Dataset**: As your application progresses through the beta testing phase, you can add runs as examples to datasets, expanding your test coverage on real-world scenarios.\n",
      "4. **Evaluation/Testing System**: LangSmith enables you to create test cases or compare with other runs, allowing for evaluation and testing of your LLM applications.\n",
      "5. **Automations**: Automations in Langsmith allow you to perform actions on traces in near real-time, such as automatically scoring traces, sending them to annotation queues, or sending them to datasets.\n",
      "\n",
      "Overall, LangSmith provides a comprehensive platform for testing and evaluating LLM applications, enabling developers to refine and improve their application's performance.\n"
     ]
    }
   ],
   "execution_count": 13
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
